// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
}
)

// 云函数入口函数
const db = cloud.database()
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()
  event.openid=wxContext.OPENID
  const _ = db.command

  return db.collection('student')
  .where({
    openid: _.eq(wxContext.OPENID)
  })
  .count()
  .then(
    res=>{
     if(res.total<9)
     {
       return db.collection('student').add({
        data:event
      })
     }
     else
     {
      return new Promise((resolve,reject) => {reject({"msg":"同一微信号提交次数不能超过10次！"})})
     }
    }
  )
  
}